package com.xlh.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;

import org.xml.sax.XMLReader;

import java.lang.reflect.Field;
import java.util.HashMap;

import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.xlh.interf.IOnClickListener;
import com.xlh.outside.MainActivity;


/**
 * Created by fang on 2017-11-10.
 */

    public class HtmlHandler implements Html.TagHandler {
    private static final String WZFONT = "wzfont";

    private int startIndex = 0;
    private int stopIndex = 0;

    private final Context mContext;
    private String cmds;



    public IOnClickListener getClickListener() {
        return clickListener;
    }

    public void setClickListener(IOnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    private IOnClickListener clickListener;

    public  HtmlHandler(Context context){
        mContext=context;
    }
    public HtmlHandler builder(){
        return this;
    }

    final HashMap<String, String> attributes = new HashMap<String, String>();

        @Override
        public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
            processAttributes(xmlReader);

            if(tag.equalsIgnoreCase(WZFONT)){
                if(opening){
                    startIndex = output.length();
                }else{
                    endFont(tag, output, xmlReader);
                }
        }
    }

    public void startFont(String tag, Editable output, XMLReader xmlReader) {
        startIndex = output.length();
    }





    public void endFont(String tag, Editable output, XMLReader xmlReader){
        stopIndex = output.length();

        String color    = attributes.get("color");
        String size     = attributes.get("size");
        cmds     = attributes.get("cmds");
        // size = size.split("px")[0];

        //if(!TextUtils.isEmpty(color) && !TextUtils.isEmpty(size)){//
           // if(!TextUtils.isEmpty(size)){
              //  output.setSpan(new AbsoluteSizeSpan(Integer.parseInt(size)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
           // }

        if (size!=null && !size.isEmpty()){
            output.setSpan(new AbsoluteSizeSpan(FUti.dip2px(mContext,Integer.parseInt(size))), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (color!=null && !color.isEmpty()){
            output.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }



        output.setSpan(new onClickSpan(), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


    }


    private class onClickSpan extends ClickableSpan implements OnClickListener{



        @Override
        public void onClick(View widget) {
                // TODO Auto-generated method stub

                //具体代码，可以是跳转页面，可以是弹出对话框，下
                // mContext.startActivity(new Intent(mContext,MainActiv面是跳转页面ity.class));
                if (cmds.isEmpty()){
                    Toast tst = Toast.makeText(mContext, "点击标签指令为空！", Toast.LENGTH_SHORT);
                    tst.setGravity(Gravity.TOP, 0, 100);
                    tst.show();
                    return;
            }

            //如果定义了外部点击接口
            if (clickListener!=null){
                clickListener.onClick(cmds);
            }

            ((MainActivity)mContext).getmSocketClient().sendMsg(cmds);


        }
    }

    /**
     * by  fang/大当家/QQ184377367
     * xmlReader 结构是传入的HTML的结构
     * 该函数主要实现解析出HTML中的节点属性
     * @param xmlReader
     */
    private void processAttributes(final XMLReader xmlReader) {
        try {
            Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
            elementField.setAccessible(true);
            Object element = elementField.get(xmlReader);
            Field attsField = element.getClass().getDeclaredField("theAtts");
            attsField.setAccessible(true);
            Object atts = attsField.get(element);
            Field dataField = atts.getClass().getDeclaredField("data");
            dataField.setAccessible(true);
            String[] data = (String[])dataField.get(atts);
            Field lengthField = atts.getClass().getDeclaredField("length");
            lengthField.setAccessible(true);
            int len = (Integer)lengthField.get(atts);

            for(int i = 0; i < len; i++){
                attributes.put(data[i * 5 + 1], data[i * 5 + 4]);
            }
        }
        catch (Exception e) {

        }
    }




}